package com.study.a01;

//import org.apache.ibatis.io.Resources;
//import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.session.SqlSessionFactoryBuilder;
//import org.junit.jupiter.api.Test;


import com.study.custom.mybatis.*;
import com.study.entity.User;
import com.study.mapper.UserMapper;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/12/20 13:05
 */
public class A01 {

  @Test
  public void testSelectOne() throws IOException {
    String resource = "sqlMapConfig.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    DefaultSqlSessionFactory sqlSessionFactory =
      new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession session = sqlSessionFactory.openSession();
    UserMapper mapper = session.getMapper(UserMapper.class);

    System.out.println(mapper.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(mapper)));

    //User user1 = mapper.selectOneUser(1L);
    //System.out.println(user1);
    List<User> users = mapper.selectAll();
    System.out.println("========>");
    System.out.println(users);

    User user = mapper.selectOneUser(1L);
    System.out.println(user);

  }

  @Test
  public void mapperProxyTest() {
    UserMapper mapper1 = (UserMapper) Proxy.newProxyInstance(UserMapper.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = "0";
        if (args != null) {
          if (args.length == 0) {
            name = "0a";
          } else {
            name = "0b";
          }
        }

        return "ok" + name;
      }
    });
    //System.out.println(mapper1);
    System.out.println(mapper1.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(mapper1)));
  }

  /**
   * 执行查询所有用户的SQL语句必须的步骤
   * 1. 需要获取连接
   * 2. 需要SQL语句
   * 3. 需要将结果集中的数据封装到JavaBean中
   */

  /**
   * 步骤1： 建立大概的结构
   * 包括 Resources SqlSession SqlSessionFactoryBuilder sqlSessionFactory UserMapper
   */

  /**
   * 步骤2：解析资源文件，将解析的数据源
   * 创建XMLConfigBuilder解析SqlMapConfig.xml
   * 创建Configuration对象，存储解析的数据库连接信息
   * 改造Configuration对象，使它具备获取数据库链接Connection的功能
   */

  /**
   * 步骤3： 解析xxMapper.xml
   * 解析UserMapper.xml获得SQL语句和返回类型的全限定名
   * 创建Mapper对象，存储SQL语句和返回类型的全限定名
   * 将解析的结果封装到Mapper中
   */

}
